﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
using System.Text.RegularExpressions;
using System.IO;

namespace ShellAnywhere.Model
{
	public class Point : IXmlSerializable
	{
		#region ctors
		public Point() { }
		public Point(int? x, int? y) { X = x; Y = y; }
		public Point(string point) { FromString(point); }
		#endregion ctors

		#region props
		public int? X { get; set; }
		public int? Y { get; set; }
		#endregion props

		#region methods
		public XmlSchema GetSchema() { return null; }
		public void ReadXml(XmlReader reader)
		{
			reader.MoveToAttribute("Point");
			string point = reader.ReadContentAsString();
			FromString(point);
		}

		public void WriteXml(XmlWriter writer)
		{
			writer.WriteStartAttribute("Point");
			writer.WriteString(ToString());
			writer.WriteEndAttribute();
		}
		public override string ToString()
		{
			string x = (X != null) ? X.ToString() : "Auto";
			string y = (Y != null) ? Y.ToString() : "Auto";
			return "{" + x + "," + y + "}";
		}
		public void FromString(string point)
		{
			Match match = Regex.Match(point, "{([A-Za-z0-9]+),([A-Za-z0-9]+)}");
			string x = match.Groups[1].ToString();
			string y = match.Groups[2].ToString();
			X = (!string.IsNullOrEmpty(x) && x != "Auto") ? Convert.ToInt32(x) : default(int?);
			Y = (!string.IsNullOrEmpty(y) && y != "Auto") ? Convert.ToInt32(y) : default(int?);
		}
		#endregion methods
	}

	[XmlRoot]
	public class Target : Param, ITarget
	{
		#region ctors
		public Target() { }
		public Target(string path, string name = null)
			: base(System.IO.Path.GetFileName(path)) //assign the var 'Name' in Param a default value
		{
			Path = path;
			Position = new Point();
			Size = new Point();
			if (name != null) //if value of 'name' passed in is not null, reassign the value of 'Name' in Param
				Name = name;
		}
		public Target(string path, string arguments, string name = null)
			: this(path, name)
		{
			Arguments = arguments;
		}
		public Target(string path, string arguments, string description, string name = null)
			: this(path, arguments, name)
		{
			Description = description;
		}
		public Target(string path, string arguments, string description, Point position, Point size, string name = null)
			: this(path, arguments, description, name)
		{
			Position = position;
			Size = size;
		}
		#endregion ctors

		#region props
		[XmlAttribute]
		public string Path { get; set; }
		[XmlAttribute]
		public string Arguments { get; set; }
		[XmlAttribute]
		public string Description { get; set; }
		[XmlAttribute]
		public Point Position { get; set; }
		[XmlAttribute]
		public Point Size { get; set; }
		[XmlIgnore]
		public string Ext { get { return System.IO.Path.GetExtension(Path); } }
		[XmlIgnore]
		public TargetType Type
		{
			get
			{
				TargetType type = TargetType.Undef;
				string ext = Ext;
				if (ext == ".exe")
					type = TargetType.Exe;
				else if (ext == ".lnk")
					type = TargetType.Lnk;
				else if (Directory.Exists(Path))
					type = TargetType.Folder;
				else if (File.Exists(Path))
					type = TargetType.File;
				return type;
			}
		}
		#endregion props

		#region methods
		public override string ToString()
		{
			return base.ToString();
		}
		#endregion methods

		#region fields
		#endregion fields
	}
}
